import axios from "axios";
import { ElLoading, ElMessage } from "element-plus";
const https = axios.create({
  baseURL: "/api",
  timeout: 1000,
});
//
https.interceptors.request.use(
  (config) => {
    // ElLoading.service({
    //   lock: true,
    //   text: "Loading",
    //   background: "rgba(255, 255, 255, 0.7)",
    //   time: 2000,
    // });
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);
//
https.interceptors.response.use(
  (response) => {
    return response.data;
  },
  (error) => {
    switch (error.response.status) {
      case 400:
        ElMessage.error("请求参数错误");
        break;
      case 401:
        ElMessage.error("请先登录");
        break;
      case 403:
        ElMessage.error("权限不足");
        break;
      case 404:
        ElMessage.error("请求的资源不存在");
        break;
      case 500:
        ElMessage.error("服务器错误");
        break;
      default:
        ElMessage.error("请求失败");
        break;
    }
    return Promise.reject(error);
  }
);
export default https;
